var co = require('co');
var seqModule = require('../../models/index');
var db = new seqModule();
var util = require('../../common/util');
var memberController = {
    login:function(req,res){
        co(function () {
            if(req.body){
                var loginName = req.body.loginName;
                var deviceId = req.body.deviceId;
                if(util.checkEmail(loginName)){
                    var params = {};
                    params.email = loginName;
                    params.filed1 = util.MD5(req.body.passWord);
                    db.memberModule.findOne({where:params}).then(function(result){
                        if(result){
                            db.memberModule.update({filed8:deviceId},{where:params}).then(function(uptResult){
                                var paramT = {};
                                paramT.id = result.id;
                                paramT.name = result.name;
                                paramT.tel = result.tel;
                                paramT.hospitalName = result.hospitalName;
                                paramT.hospitalOffices = result.hospitalOffices;
                                res.status(200).json(paramT)
                            })
                        }else{
                            res.status(412).json({"message":"用户名或密码错误！"})
                        }
                    })
        
                }else if(util.valPhone(loginName)){
                    var params = {};
                    params.tel = loginName;
                    params.filed1 = util.MD5(req.body.passWord);
                    db.memberModule.findOne({where:params}).then(function(result){
                        if(result){
                            db.memberModule.update({filed8:deviceId},{where:params}).then(function(uptResult){
                                var paramT = {};
                                paramT.id = result.id;
                                paramT.name = result.name;
                                paramT.tel = result.tel;
                                paramT.hospitalName = result.hospitalName;
                                paramT.hospitalOffices = result.hospitalOffices;
                                res.status(200).json(paramT)
                            })
                        }else{
                            res.status(412).json({"message":"用户名或密码错误！"})
                        }
                    })
        
                }else{
                    res.status(412).json({"message":"用户名或密码错误！"})
                }
            }else{
                res.status(500).json({ "message":"参数错误！" });
            }
        
    }).catch(function (e) {
        res.status(500).json({ "message":"服务错误！" });
    })
       
    },
    register:function(req,res){
        co(function () {
            var tel = req.body.tel;
            db.memberModule.findOne({where:{tel:tel}}).then(function(result){
                if(result){
                    res.status(409).json({ "message":"该手机号已注册！" });
                }else{
                    var paramT = {};
                    paramT.where = {"tel":tel,"code":req.body.code};
                    db.sendCodeModule.findOne(paramT).then(function(sendCode){
                        if(sendCode){
                            req.body.id = util.getUUID();
                            req.body.createTime = util.getNowTime();
                            req.body.filed1 = util.MD5(req.body.password);
                           db.memberModule.create(req.body).then(function(resultAdd){
                               delete resultAdd.filed1;
                            res.status(200).json(resultAdd);
                           })
                        }else{
                            res.status(403).json({ "message":"验证码错误！" });
                        }
                    })
                   
                }
            });
        }).catch(function (e) {
            res.status(500).json({ "message":"服务错误！" });
        })
    },
    updPassWord:function(req,res){
        co(function () {
            var params = {};
            params.filed1 = util.MD5(req.body.newPassWord);
           db.memberModule.update(params,{where:req.params}).then(function(result){
                res.status(200).json({ "message":"修改成功！" });
           })
        }).catch(function (e) {
            res.status(500).json({ "message":"服务错误！" });
        })
    },
    getmemberPage: function (req, res) {
        co(function () {
            var page = parseInt(req.query.page);
            var params = {};
            var paramT = {};
            // var include = [{ model: db.wxUsersModule }];
            // paramT.include = include;
            //根据id
            if (req.query.id != undefined) {
                params["id"] = req.query.id;
            }
            if (req.query.openId != undefined) {
                params["openId"] = req.query.openId;
            }
            //模糊查询
            if (req.query.name != undefined) {
                params["name"] = { "$like": "%" + req.query.name + "%" };
            }
            if (req.query.tel != undefined) {
                params["tel"] = { "$like": "%" + req.query.tel + "%" };
            }
            if (req.query.pTitle != undefined) {
                params["$or"] = { "name": { "$like": "%" + req.query.pTitle + "%" }, "tel": { "$like": "%" + req.query.pTitle + "%" } };
            }
            if (req.query.type != undefined) {
                params["type"] = parseInt(req.query.type);
            }
            if (req.query.province != undefined) {
                params["province"] = { "$like": "%" + req.query.province + "%" };

            } if (req.query.city != undefined) {
                params["city"] = { "$like": "%" + req.query.city + "%" };
            }
            if (req.query.cTitle != undefined) {
                params["$or"] = { "province": { "$like": "%" + req.query.cTitle + "%" }, "city": { "$like": "%" + req.query.cTitle + "%" } };
            }
            if (req.query.hospitalName != undefined) {
                params["hospitalName"] = { "$like": "%" + req.query.hospitalName + "%" };
            }
            if (req.query.hospitalOffices != undefined) {
                params["hospitalOffices"] = { "$like": "%" + req.query.hospitalOffices + "%" };
            }
            if (req.query.wxTitle != undefined) {
                params["$or"] = { "name": { "$like": "%" + decodeURI(req.query.wxTitle) + "%" }, "hospitalName": { "$like": "%" + decodeURI(req.query.wxTitle) + "%" }, "hospitalOffices": { "$like": "%" + decodeURI(req.query.wxTitle) + "%" } };
            }
            if (req.query.filed1 != undefined) {
                params["filed1"] = { "$like": "%" + req.query.filed1 + "%" };
            }
            if (req.query.filed2 != undefined) {
                params["filed2"] = { "$like": "%" + req.query.filed2 + "%" };
            }
            if (req.query.filed3 != undefined) {
                params["filed3"] = { "$like": "%" + req.query.filed3 + "%" };
            }
            if (req.query.filed4 != undefined) {
                params["filed4"] = { "$like": "%" + req.query.filed4 + "%" };
            }
            if (req.query.filed5 != undefined) {
                params["filed5"] = { "$like": "%" + req.query.filed5 + "%" };
            }
            if (req.query.filed8 != undefined) {
                params["filed8"] = req.query.filed8;
            }
            paramT["where"] = params;
            paramT["order"] = [["createTime", 'DESC']];
            if (req.query.page != undefined && req.query.rows != undefined) {
                paramT["offset"] = parseInt(req.query.page);
                paramT["limit"] = parseInt(req.query.rows);
            }
            db.memberModule.findAndCountAll(paramT).then(function (result) {
                result.page = page;
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })
    }
}

module.exports = memberController;